/*
 * (swing1.1beta3)
 */
package jp.gr.java_conf.tame.swing.table;

 
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

/**
 * The Class AttributiveCellRenderer.
 *
 * @version 1.0 11/22/98
 */

public class AttributiveCellRenderer extends JLabel
    implements TableCellRenderer {
  
  /** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

/** The no focus border. */
protected static Border noFocusBorder; 
 
  /**
   * Instantiates a new attributive cell renderer.
   */
  public AttributiveCellRenderer() {
    noFocusBorder = new EmptyBorder(1, 2, 1, 2);
    setOpaque(true);
    setBorder(noFocusBorder);  
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
   */
  public Component getTableCellRendererComponent(JTable table, Object value,
                 boolean isSelected, boolean hasFocus, int row, int column) {
    Color foreground = null;
    Color background = null;
    Font font = null;
    TableModel model = table.getModel();
    if (model instanceof AttributiveCellTableModel) {
      CellAttribute cellAtt = ((AttributiveCellTableModel)model).getCellAttribute();
      if (cellAtt instanceof ColoredCell) {
	foreground = ((ColoredCell)cellAtt).getForeground(row,column);
	background = ((ColoredCell)cellAtt).getBackground(row,column);
      }
      if (cellAtt instanceof CellFont)
		font = ((CellFont)cellAtt).getFont(row,column);
    }
  
      setForeground((foreground != null) ? foreground 
			  : table.getForeground());
      setBackground((background != null) ? background 
			  : table.getBackground());

    setFont((font != null) ? font : table.getFont());
    

   
      setBorder(new LineBorder(new Color(102,102,102)));
  
    setValue(value);        
    return this;
  }
    
  /**
   * Sets the value.
   *
   * @param value the new value
   */
  protected void setValue(Object value) {
    setText((value == null) ? "" : value.toString());
  }
}


